package com.itcast.activiti.event.listener;

import com.alibaba.fastjson.JSONObject;
import com.itcast.activiti.domain.AddSign;
import com.itcast.activiti.event.AddSignEvent;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 审批结果通知
 */
@Slf4j
@Component
public class AddSignListener {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Value("${activiti.rocketmq.topic.activiti}")
    private String topic;

    @EventListener
    public void execute(AddSignEvent event) {
        JSONObject jsonObject = new JSONObject();
        AddSign addSign = event.getAddSign();
        jsonObject.put(AddSign.Fields.passbackParam, addSign.getPassbackParam());
        jsonObject.put("massageType", "ADD_SIGN");
        jsonObject.put(AddSignEvent.Fields.tenantId, event.getTenantId());
        jsonObject.put(AddSignEvent.Fields.businessKey, event.getBusinessKey());
        jsonObject.put(AddSignEvent.Fields.assignee, event.getAssignee());
        jsonObject.put(AddSignEvent.Fields.addSign, event.getAddSign());
        rocketMQTemplate.convertAndSend(String.format("%s:%s", topic, "TAG_TASK_ADD_SIGN"), jsonObject);
    }
}
